const path = require("path");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
// const CopyWebpackPlugin = require("copy-webpack-plugin");

// class MyPlugin {
//   apply(compiler) {
//     console.log("Myplugin 启动111");
//     // console.log(compiler);
//     compiler.hooks.emit.tap("MyPlugin", (compilation) => {
//       for (const name in compilation.assets) {
//         // console.log(name);
//         // console.log(compilation.assets[name].source());
//         if (name.endsWith(".js")) {
//           const contents = compilation.assets[name].source();
//           const withoutComments = contents.replace(/\/\*\*+\*\//g, "");
//           compilation.assets[name] = {
//             source: () => withoutComments,
//             size: () => withoutComments.length,
//           };
//         }
//       }
//     });
//   }
// }
module.exports = {
  entry: path.join(__dirname, "./src/main.js"),
  output: {
    filename: "bundle.js",
    path: path.join(__dirname, "dist"),
    // publicPath: "dist/",
  },
  devServer:{
    contentBase:['public'],
    proxy:{
      '/api':{
        target:"https://api.github.com",
        pathRewrite:{
          '^/api':""
        },
        changeOrigin:true
      }
    }
  },
  module: {
    rules: [
      {
        test: /.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /.png$/,
        use: {
          loader: "url-loader",
          options: {
            limit: 10 * 1024, // 10kb
          },
        },
      },
    ],
  },
  plugins: [
    // new CleanWebpackPlugin(),
    // 用于生成index.html
    new HtmlWebpackPlugin({
      title: "webpack-plugin",
      meta: {
        viewport: "width=device-width",
      },
      template: "./src/index.html",
    }),
    // 用于生成about.html
    new HtmlWebpackPlugin({
      filename: "about.html",
    }),
    // new CopyWebpackPlugin(["public"]),
    // new MyPlugin(),
  ],
  mode: "none",
};
